/*
 * Copyright 2013 Sebastian Gesemann <s.gesemann@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * This is just a small test for the FFT code.
 */

#include <cmath>
#include <iostream>
#include <vector>
#include <ctime>

#include "array_ref.hpp"
#include "fft.hpp"

namespace {

using std::cout;
using std::endl;
using std::vector;

using sg::complx;
using sg::real;
using sg::array_ref;

complx cutnoise(complx const& in, real eps)
{
	using std::abs;
	return complx(
		abs(in.real())<eps ? 0 : in.real(),
		abs(in.imag())<eps ? 0 : in.imag()
	);
}

void show(array_ref<const complx> data, real eps)
{
	for (unsigned i=0; i<data.size(); ++i) {
		cout << cutnoise(data[i],eps) << endl;
	}
}

void perf_test()
{
	vector<complx> data;
	const int n = 1024;
	const int p = 48000;
	data.reserve(n);
	for (int k=0; k<n; ++k) {
		data.push_back(complx(k%31-15,k%29-14));
	}
	auto c1 = std::clock();
	for (int pass=0; pass<p; ++pass) {
		sg::fft(data);
		sg::ifft(data);
	}
	auto c2 = std::clock();
	cout << static_cast<double>(c2-c1)/CLOCKS_PER_SEC << endl;
}

} // anonymous namespace

int main()
{
	vector<complx> data (8,complx(0,0));
	data[1] = complx(1,0);
	show(data,5e-7);
	sg::fft(data);
	cout << "---\n";
	show(data,5e-7);
	sg::ifft(data);
	cout << "---\n";
	show(data,5e-7);
	cout << "---\n";
	perf_test();
}

